<?php namespace system\model;

use hdphp\Model\Model;

class Category extends Model{

	//数据表
	protected $table = "category";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
        ['cname','required','请输入分类名称',3,3],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;


	//禁止插入的字段
	protected $denyInsertFields = [];

	//禁止更新的字段
	protected $denyUpdateFields = [];

	//前置方法 比如: _before_add 为添加前执行的方法
	protected function _before_add(){}
	protected function _before_delete(){}
	protected function _before_save(&$data){}

	protected function _after_add(){}
	protected function _after_delete(){}
	protected function _after_save(){}

    public function getfu($cid){
        $cids=$this->getson($this->get(),$cid);
        $cids[]=$cid;
        $data=$this->whereNotIn('cid',$cids)->get();
        return Data::tree($data,'cname');

    }
    public function getson($data,$cid){
        static $temp=[];
        foreach($data as $k=>$v){
            if($v['pid']==$cid){
                $temp[]=$v['cid'];
                $this->getson($data,$v['cid']);
            }
        }return $temp;
    }
//    分类修改
    public function edit(){
        if(!$this->create()) return false;
        //p($_POST);die;
        return $this->save();
    }
    //分类添加
    public function store(){
        if(!$this->create()) return false;
        $data=$this->where('cname',$_POST['cname'])->first();
        if($data){
            $this->error='已有此分类名称，请勿重复输入';return false;
        }else{
            $this->add();
        }
        return true;
    }

    public function add_son(){
        if(!$this->create()) return false;
        $pid=q('get.cid',0,'intval');
        $_POST['pid']=$pid;
        //p($_POST);die;
        $this->add();
        return true;
    }
    public function addsort(){
        p($_POST);die;
    }

}